package com.shwread.android.ui.customview;

import android.content.Context;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.view.MotionEvent;

public class QYViewPager extends ViewPager {

    private boolean isSliding = true;
    private float mDownX;
    private float mDownY;

    public QYViewPager(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    //	@Override
//	public boolean dispatchTouchEvent(MotionEvent arg0) {
//		//告诉父view，我的事件自己处理(解决ViewPager嵌套引起子ViewPager无法触摸问题)
//		getParent().requestDisallowInterceptTouchEvent(true);
//
//		return super.dispatchTouchEvent(arg0);
//	}
    public void setSliding(boolean isSliding) {
        this.isSliding = isSliding;
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent arg0) {
        return isSliding ? super.onInterceptTouchEvent(arg0) : false;
    }

    @Override
    public boolean onTouchEvent(MotionEvent arg0) {
        return isSliding ? super.onTouchEvent(arg0) : false;
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        switch (ev.getAction()) {
            case MotionEvent.ACTION_DOWN:
                mDownX = ev.getX();
                mDownY = ev.getY();
                getParent().requestDisallowInterceptTouchEvent(true);
                break;
            case MotionEvent.ACTION_MOVE:
                if (Math.abs(ev.getX() - mDownX) > Math.abs(ev.getY() - mDownY)) {
                    getParent().requestDisallowInterceptTouchEvent(true);
                } else {
                    getParent().requestDisallowInterceptTouchEvent(false);
                }
                break;
            case MotionEvent.ACTION_UP:
            case MotionEvent.ACTION_CANCEL:
                getParent().requestDisallowInterceptTouchEvent(false);
                break;

            default:
                break;
        }
        return super.dispatchTouchEvent(ev);
    }
}
